import scrapy
import json


class FanyiSpider(scrapy.Spider):
    name = 'fanyi'
    allowed_domains = ['fanyi.baidu.com']

    # post请求，如果没有参数，直接请求是没有意义的
    # 所以start_urls没有用了
    # 同理parse方法也没用了
    # start_urls = ['http://fanyi.baidu.com']
    #
    # def parse(self, response):
    #     pass
    def start_requests(self):
        url = 'http://fanyi.baidu.com/sug'

        data = {
            'kw': 'final'
        }

        yield scrapy.FormRequest(url=url, formdata=data, callback=self.parse_secong)

    def parse_secong(self, response):
        content = response.text
        obj = json.loads(content, encoding='utf-8')
        print(obj)
